package com.feign.util;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;

/**
 * @author zw
 * @describe 标准输出流的线程类,用于捕获 执行cmd命令时错误流写入导致的阻塞问题
 * @date 2022-10-30.
 */
@Slf4j
@AllArgsConstructor
public class SystemStream implements Callable {
    InputStream inputStream;
    @Override
    public String call() throws IOException {
       //待补全代码
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            while ((line=bufferedReader.readLine())!=null) {
//                log.debug(line);
                stringBuilder.append(line);
            }
        }finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                inputStreamReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
       return stringBuilder.toString();
    }
}